package com.qiudaozhang.jblog.backend.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.qiudaozhang.jblog.article.service.ArticleService;
import com.qiudaozhang.jblog.article.service.CategoryService;
import com.qiudaozhang.jblog.article.service.TagService;
import com.qiudaozhang.jblog.common.util.CommonCode;
import com.qiudaozhang.jblog.model.Article;
import com.qiudaozhang.jblog.model.ArticleInfo;
import com.qiudaozhang.jblog.model.Category;
import com.qiudaozhang.jblog.model.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 邱道长
 * 2018/12/11 16:54
 * 文章控制器
 */
@Controller
@RequestMapping("admin/article")
public class ArticleController {


    @Reference(url = "${article.service.provider.url}", version = "${service.version}", group = "${dubbo.group}")
    private ArticleService articleService;

    @Reference(url = "${article.service.provider.url}", version = "${service.version}", group = "${dubbo.group}")
    private TagService tagService;

    @Reference(url = "${article.service.provider.url}", version = "${service.version}", group = "${dubbo.group}")
    private CategoryService categoryService;

    /**
     * 提交文章
     *
     * @param article 文章model
     * @param model
     * @return
     */
    @PostMapping("post")
    public String index(ArticleInfo article, Model model) {
        Integer id = articleService.save(article);
        return "redirect:/admin/article/get/" + id;
    }


    /**
     * 查看文章
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("get/{id}")
    public String get(@PathVariable("id") Integer id, Model model) {
        Article a = articleService.getById(id);
        model.addAttribute("article", a);
        return "admin/article/article";
    }

    /**
     * 跳转到编写文章页面
     *
     * @return
     */
    @RequestMapping("write")
    public String write(Model model) {
        // 查询所有可用标签
        List<Tag> tags = tagService.selectAll();
        model.addAttribute("tags", tags);
        // 查询所有分类信息
        List<Category> categories = categoryService.selectAll();
        model.addAttribute("categories",categories);
        return "admin/article/write";
    }


    @RequestMapping("index")
    public String list() {
        return "admin/article/list";
    }

    /**
     * 获取文章列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> query(@RequestParam("page") int pageNum, @RequestParam("limit") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        PageInfo<Article> p = articleService.queryPage(pageNum, pageSize);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", p.getList());
        map.put("count", p.getTotal());
        return map;

    }


    /**
     * 删除文章
     * @param articleId
     * @return
     */
    @RequestMapping("delete/{articleId}")
    @ResponseBody
    public String deleteById(@PathVariable("articleId") Integer articleId) {
        articleService.delete(articleId);
        return CommonCode.SUCCESS;
    }

    /**
     * 编辑文章
     * @param articleId
     * @param model
     * @return
     */
    @RequestMapping("edit/{articleId}")
    public String editById(@PathVariable("articleId") Integer articleId,Model model){
        ArticleInfo articleInfo = articleService.getDetail(articleId);
        model.addAttribute("articleInfo",articleInfo);
        //查询所有分类信息
        List<Category> categories = categoryService.selectAll();
        model.addAttribute("categories",categories);
        //查询所有的标签信息
        List<Tag> tags = tagService.selectAll();
        model.addAttribute("tags",tags);
        return "admin/article/edit";
    }

    /**
     * 更新文章
     * @return
     */
    @RequestMapping("update")
    public String update(ArticleInfo articleInfo){
        // 更新后直接去文章展示页面
        articleService.updateDetail(articleInfo);
        return "redirect:/admin/article/get/"+articleInfo.getArticleId();
    }
}
